##rem $Id: HSPascalUnit.sd,v 1.2 1999/11/28 03:36:41 carlos Exp $
##shortstrings
##stringtype E
UNIT %bLocale;
INTERFACE

{****************************************************

  This file was created automatically by '%fv'
  from "%f0".

  Do NOT edit by hand!

****************************************************}

USES
  Exec , Locale , Utility ;

CONST
  %i = %d ;\n  %i_STR = %s%[#0;\n

PROCEDURE CloseCatalog;

PROCEDURE OpenCatalog(loc : pLocale ; language : STRPTR ) ;

FUNCTION GetString(num : LONGINT) : STRPTR ;

FUNCTION GetPasString(num : LONGINT) : STRING;

IMPLEMENTATION 

CONST
  builtinlanguage = %l%[#0;
  version = %v ;
  catalog  : pCatalog = NIL ;

VAR

  OldExitProc : Pointer;

TYPE

  tAppString = RECORD
     id  : LONGINT ;
     str : STRPTR ;
  END ;

  tAppStringArray = ARRAY [0..%n] OF tAppString ;

CONST
  AppStrings : tAppStringArray = (( id : 0 ; str : NIL ));  

FUNCTION CPtr( s : string ):pointer;INLINE
  $201F, { move.l (a7)+,d0 ; get address string }
  $5280, { addq.l #1,d0    ; ignore counter }
  $2E80; { move.l d0,(a7)  ; return "C-string" address }

PROCEDURE CloseCatalog;
BEGIN
  IF catalog <> NIL THEN
    BEGIN
      Locale.CloseCatalog(catalog) ;
      catalog := NIL
    END;
END; 

PROCEDURE InitAppStringsArray;
BEGIN
  AppStrings[%d].id := %i;\n  AppStrings[%d].str := CPtr(%i_STR);\n
END;

PROCEDURE OpenCatalog(loc : pLocale ; language : STRPTR );
VAR
   tags   : ARRAY [0..7] OF LONGINT ;
BEGIN
  CloseCatalog;
  IF (catalog = NIL) AND (LocaleBase <> NIL) THEN
    BEGIN
      IF language <> NIL THEN
        BEGIN
          tags[0] := OC_BuiltInLanguage; tags[1] := long(CPtr(builtinlanguage));
          tags[2] := OC_Language ;       tags[3] := long(language); 
          tags[4] := OC_Version ;        tags[5] := version;
          tags[6] := TAG_END;
        END
      ELSE
        BEGIN
          tags[0] := OC_BuiltInLanguage; tags[1] := long(CPtr(builtinlanguage));
          tags[2] := OC_Version ;        tags[3] := version;
          tags[4] := TAG_END;
        END;
    catalog := Locale.OpenCatalogA(loc, CPtr('%b.catalog'#0), @tags) ;
    END 
END;

FUNCTION GetString(num : LONGINT) : STRPTR ;

VAR
  i       : LONGINT ;
  default : STRPTR ;

BEGIN
  i := 0 ;

  WHILE (i < %n) AND (AppStrings[i].id <> num) DO
  BEGIN
    INC(i)
  END ;

  IF i <> %n THEN
    default := AppStrings[i].str
   ELSE
    default := NIL ;

  IF catalog <> NIL THEN
    GetString := Locale.GetCatalogStr(catalog, num, default)
   ELSE
    GetString := default
END;

FUNCTION GetPasString(num : LONGINT) : STRING;
TYPE
  pa = ^ta;
  ta = PACKED ARRAY [0..MAXINT] OF CHAR;
VAR
  i    : INTEGER;
  sptr : pa;
  s    : STRING;
BEGIN
  sptr := pa(GetString( num ));
  s := '';
  i := 0;
  WHILE sptr^[i] <> #0 DO 
  BEGIN
    s := s + sptr^[i];
    INC(i)
  END;
  GetPasString := s
END;

(***************************************************************************)
(*                  << INIT AND EXIT PROCEDURE  >>                         *)
(***************************************************************************)

PROCEDURE InitFunction;
BEGIN
  LocaleBase := OpenLibrary( 'locale.library' , 38 );
  if LocaleBase = NIL then 
    BEGIN
      WRITELN( '*** ERROR *** locale.library ' );
      HALT
    END;
  InitAppStringsArray;
  OpenCatalog( NIL , NIL );
END;

PROCEDURE ExitFunction;
BEGIN
  if LocaleBase <> NIL then CloseLibrary( LocaleBase );
  CloseCatalog;
END;

PROCEDURE ExitHandler;
BEGIN
  ExitProc := OldExitProc;
  ExitFunction;
END;

PROCEDURE Initialize;
BEGIN
  OldExitProc := ExitProc;
  ExitProc := @ExitHandler;
  InitFunction;
END;

BEGIN
  Initialize;
END.
